package com.ruoyi.product.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.redisson.service.impl.RedissonServiceImpl;
import com.ruoyi.product.domain.MallProductRuleCenter;
import com.ruoyi.product.domain.MallProductStockLog;
import com.ruoyi.product.domain.RedissonConstants;
import com.ruoyi.product.mapper.MallProductRuleCenterMapper;
import com.ruoyi.product.response.MallProductStockLogResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.MallProductStockLogMapper;

import com.ruoyi.product.service.IMallProductStockLogService;

/**
 * 商品库存录入记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-11
 */
@Service
public class MallProductStockLogServiceImpl implements IMallProductStockLogService
{
    @Autowired
    private MallProductStockLogMapper mallProductStockLogMapper;
    @Autowired
    private MallProductRuleCenterMapper mallProductRuleCenterMapper;
    @Autowired
    private RedissonServiceImpl redissonService;
    /**
     * 查询商品库存录入记录
     *
     * @param id 商品库存录入记录主键
     * @return 商品库存录入记录
     */
    @Override
    public
    MallProductStockLogResponse selectMallProductStockLogById(String id)
    {
        return mallProductStockLogMapper.selectMallProductStockLogById(id);
    }

    /**
     * 查询商品库存录入记录列表
     *
     * @param mallProductStockLog 商品库存录入记录
     * @return 商品库存录入记录
     */
    @Override
    public List<MallProductStockLogResponse> selectMallProductStockLogList(MallProductStockLog mallProductStockLog)
    {
        return mallProductStockLogMapper.selectMallProductStockLogList(mallProductStockLog);
    }

    /**
     * 新增商品库存录入记录
     *
     * @param mallProductStockLog 商品库存录入记录
     * @return 结果
     */
    @Override
    public int insertMallProductStockLog(MallProductStockLog mallProductStockLog)
    {
        mallProductStockLog.setId(IdUtils.simpleUUID());
        mallProductStockLog.setCreateBy(SecurityContextHolder.getUserId().toString());
        mallProductStockLog.setCreateTime(DateUtils.getNowDate());
        synchronized (this){
            //根据商品的规模id查询mall_product_rule_center中规模对象和库存
            String productSkuId = mallProductStockLog.getProductSkuId();
            //分布式锁获取锁
            redissonService.lock(RedissonConstants.INSERT_PRODUCT_STOCK,productSkuId,RedissonConstants.TIME_EXPIRE,RedissonConstants.RETRY_TIMES,RedissonConstants.RETRY_INTERVAL);
            MallProductRuleCenter productRuleCenter = mallProductRuleCenterMapper.selectMallProductRuleCenterById(productSkuId);
            //获取查询的商品库存
            Long stock = productRuleCenter.getStock();
            //前端参数的商品库存
            Long stockNum = mallProductStockLog.getStockNum();
            //最新的商品库存
            stock = stock+stockNum;
            //进行商品库存的修改(根据id)
            MallProductRuleCenter updateProSku  = new MallProductRuleCenter();
            updateProSku.setId(mallProductStockLog.getProductSkuId());
            updateProSku.setStock(stock);
            //修改数据库
            mallProductRuleCenterMapper.updateMallProductRuleCenter(updateProSku);
            //释放锁
            redissonService.unlock(RedissonConstants.INSERT_PRODUCT_STOCK,productSkuId);
        }
        //添加到记录表
        return mallProductStockLogMapper.insertMallProductStockLog(mallProductStockLog);
    }

    /**
     * 修改商品库存录入记录
     *
     * @param mallProductStockLog 商品库存录入记录
     * @return 结果
     */
    @Override
    public int updateMallProductStockLog(MallProductStockLog mallProductStockLog)
    {
        return mallProductStockLogMapper.updateMallProductStockLog(mallProductStockLog);
    }

    /**
     * 批量删除商品库存录入记录
     *
     * @param ids 需要删除的商品库存录入记录主键
     * @return 结果
     */
    @Override
    public int deleteMallProductStockLogByIds(String[] ids)
    {
        return mallProductStockLogMapper.deleteMallProductStockLogByIds(ids);
    }

    /**
     * 删除商品库存录入记录信息
     *
     * @param id 商品库存录入记录主键
     * @return 结果
     */
    @Override
    public int deleteMallProductStockLogById(String id)
    {
        return mallProductStockLogMapper.deleteMallProductStockLogById(id);
    }
}
